package homework0708_2;

import homework0708_jk_2.RegisterService;

import java.util.Scanner;

/**
 * 实现注册接口，实现类
 */
public class RegisterServiceImpl implements RegisterService {
    Scanner scanner = new Scanner(System.in);
    String iptUserName;//输入的用户名
    String iptPwd;//输入的密码
    String iptPwd1;//确认密码
    String iptEmail;//输入的邮箱
    boolean iptPwdLegally;//输入密码是否合法
    boolean iptPwdState;//俩次输入密码是否相同
    boolean iptEmailLegally;//输入邮箱是否合法

    @Override
    public String register() {
        while (true) {
            System.out.println("请输入要注册的用户名");
            iptUserName = scanner.next();//用户名
            //输入账号是否合法
            boolean iptUserNameLegally = iptUserName.matches("^(?![\\d]+$)(?![a-zA-Z]+$)(?![^\\da-zA-Z]+$).{6,20}$");
            System.out.println("请输入注册的密码");
            iptPwd = scanner.next();//密码
            iptPwdLegally = iptPwd.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$");
            if (!iptUserNameLegally || !iptPwdLegally) {
                System.out.println("用户名或密码不合法，请重新输入");
                continue;
            } else if (iptUserName.equals(iptPwd)) {
                System.out.println("用户名和密码不能相同，请重新输入");
                continue;
            }
            System.out.println("再次输入密码");
            iptPwd1 = scanner.next();//确认的密码
            if (iptPwd1.equals(iptPwd)) {
                iptPwdState = true;
            } else {
                System.out.println("俩次密码不一致，请重新输入");
                continue;
            }
            while (iptPwdState) {
                System.out.println("请输入邮箱");
                iptEmail = scanner.next();//邮箱
                iptEmailLegally = iptEmail.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
                if (iptEmailLegally) {
                    return "注册成功\n用户名：" + iptUserName + "\n密码：" + iptPwd + "\n邮箱：" + iptEmail;
                } else {
                    System.out.println("邮箱不合法,请重新输入");
                }
            }
        }
    }
}

